import Taro from '@tarojs/taro'
import { request } from './request'

export interface LoginResponse {
  token?: string
}

export interface LoginPayload {
  phone: string
  code: string
}

export function sendLoginCode(phone: string) {
  return request({
    url: '/lowcode-user/api/user/sendCode',
    data: { phone }
  })
}

export async function loginWithCode(payload: LoginPayload) {
  const data = await request<LoginResponse>({
    url: '/lowcode-user/api/user/code/login',
    data: payload
  })
  if (data?.token) {
    Taro.setStorageSync('token', data.token)
  }
  return data
}
